﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace _MD5加密
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = string.Empty;
            while (s != "exit")
            {
                s = Console.ReadLine();
                Console.WriteLine("加密前内容是:" + s);
                string ss = GetMD5(s);
                Console.WriteLine("加密后的内容:" + ss);
            }
            Console.ReadKey();
        }

        /// <summary>
        /// MD5加密字符串
        /// </summary>
        /// <param name="s">待加密字符串</param>
        /// <returns></returns>
        public static string GetMD5(string s)
        {
            StringBuilder sb = new StringBuilder();
            using (MD5 md5 = MD5.Create())
            {
                byte[] buffers = System.Text.Encoding.UTF8.GetBytes(s);
                byte[] bytes = md5.ComputeHash(buffers);
                md5.Clear();
                for (int i = 0; i < bytes.Length; i++)
                {
                    sb.Append(bytes[i].ToString("X2"));
                }
                return sb.ToString();
            }
        }
    }
}
